package com.sxc.oa.web.action;

import com.sxc.oa.utils.DButil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class QueryMemberServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String memberId = request.getParameter("memberId");
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DButil.getConnction();
            String sql = "SELECT id, name, zhuanye, deptno1 FROM sss WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, memberId);
            rs = ps.executeQuery();
            if (rs.next()) {
                String name = rs.getString("name");
                String zhuanye = rs.getString("zhuanye");
                String deptno1 = rs.getString("deptno1");

                response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();
                out.print("<!DOCTYPE html>");
                out.print("<html lang='en'>");
                out.print("<head>");
                out.print("<meta charset='UTF-8'>");
                out.print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
                out.print("<title>查询成员</title>");
                out.print("</head>");
                out.print("<body>");
                out.print("<h1>成员信息</h1>");
                out.print("<p>成员ID: " + memberId + "</p>");
                out.print("<p>成员姓名: " + name + "</p>");
                out.print("<p>成员专业: " + zhuanye + "</p>");
                out.print("<p>部门编号: " + deptno1 + "</p>");
                out.print("</body>");
                out.print("</html>");
            } else {
                response.sendRedirect(request.getContextPath() + "/error.html");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DButil.close(conn, ps, rs);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}